from libs.base.decorators import check_login, check_form
from libs.base.web import BaseHandler
from . import forms
from . import models


class StoreListHandler(BaseHandler):

    @check_login()
    @check_form(forms.StoreListForm)
    async def get(self):
        """
        @api {GET} /api/v1.0/demo/store/list 获取门店列表
        @apiVersion 1.0.0
        @apiName GetStoreList
        @apiGroup DemoStore
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) page  查询页码
        @apiParam (Request-params) limit  每页显示条数
        @apiParam (Request-params) [order]  排序字段
            (e.g. "store_status+" or "store_status-", 结尾+或无符号代表递增, -代表递减)
        @apiParam (Request-params) [store_name]  门店名称
        @apiParam (Request-params) [store_id]  门店ID
        @apiParam (Request-params) [user_id]  门店管理员
        @apiParam (Request-params) [store_status]  门店状态(0.闭店, 1.营业, 2.装修中)
        @apiParam (Request-params) [province_code]  区域编码-省份
        @apiParam (Request-params) [city_code]  区域编码-城市
        @apiParam (Request-params) [delay]  延时n秒后返回结果

        @apiParamExample {json} Request-Example:
        {
            "page": 1,
            "limit": 1,
            "order": "store_status+",
            "store_name": "闵行店",
            "second_category": "110110",
            "store_id": 1,
            "user_id": 1,
            "store_status": 1,
            "province_code": "101109",
            "city_code": "101109101",
            "delay": 1
        }

        @apiSuccess (Response-data) page  查询页码
        @apiSuccess (Response-data) limit  每页显示条数
        @apiSuccess (Response-data) total  数据总数
        @apiSuccess (Response-data) results  门店信息列表

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": {
                "results": [
                    {
                        "store_id": 2,  # 门店ID
                        "store_name": "上海徐家汇A2店",  # 门店名称
                        "country_code": "101",  # 区域编码-国家
                        "province_code": "101109",  # 区域编码-省份
                        "city_code": "101109102",  区域编码-城市
                        "store_address": "上海市闸北区天通庵路320号",  # 门店地址
                        "brand_count": 10,  # 品牌数量
                        "user_id": {  # 门店管理员
                            "user_id": 1,
                            "name": "张三"
                        },
                        "store_status": 1,  # 门店状态(0.闭店, 1.营业, 2.装修中)
                        "store_images": "",  # 门店图片
                        "create_by": {  # 数据创建者
                            "user_id": 1,
                            "name": "张三"
                        },
                        "create_time": "2019-06-25 10:26:33"  # 数据创建时间
                    }
                ],
                "total": 2,
                "page": 1,
                "limit": 10
            }
        }
        """

        model = models.StoreListModel(self)

        res = await model.fetch_list(self.form_data)

        self.finish_auto(**res)


class StoreInfoHandler(BaseHandler):

    @check_login()
    @check_form(forms.GetStoreForm)
    async def get(self, store_id):
        """
        @api {GET} /api/v1.0/demo/store/:store_id 获取门店基本信息
        @apiVersion 1.0.0
        @apiName GetStoreInfo
        @apiGroup DemoStore
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) store_id  门店ID
        @apiParam (Request-params) [delay]  延时n秒后返回结果

        @apiParamExample {json} Request-Example:
        {
            "delay": 1
        }

        @apiSuccessExample {json} Success-Response:
        {
            "status": 0,
            "message": "请求成功",
            "data": {
                "store_id": 2,  # 门店ID
                "store_name": "上海徐家汇A2店",  # 门店名称
                "country_code": "101",  # 区域编码-国家
                "province_code": "101109",  # 区域编码-省份
                "city_code": "101109102",  区域编码-城市
                "store_address": "上海市闸北区天通庵路320号",  # 门店地址
                "brand_count": 10,  # 品牌数量
                "user_id": {  # 门店管理员
                    "user_id": 1,
                    "name": "张三"
                },
                "store_status": 1,  # 门店状态(0.闭店, 1.营业, 2.装修中)
                "store_images": "",  # 门店图片
                "create_by": {  # 数据创建者
                    "user_id": 1,
                    "name": "张三"
                },
                "create_time": "2019-06-25 10:26:33"  # 数据创建时间
            }
        }
        """
        model = models.StoreInfoModel(self, store_id)
        res = await model.fetch_info(self.form_data)
        self.finish_auto(**res)

    @check_login()
    @check_form(forms.AddStoreForm)
    async def post(self, store_id):
        """
        @api {POST} /api/v1.0/demo/store 新增门店
        @apiVersion 1.0.0
        @apiName AddStoreInfo
        @apiGroup DemoStore
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) store_name  门店名称
        @apiParam (Request-params) country_code  区域编码-国家
        @apiParam (Request-params) province_code  区域编码-省份
        @apiParam (Request-params) city_code  区域编码-城市
        @apiParam (Request-params) store_address  门店地址
        @apiParam (Request-params) brand_count  品牌数量
        @apiParam (Request-params) user_id  门店管理员
        @apiParam (Request-params) store_status  门店状态

        @apiParamExample {json} Request-Example:
        {
            "store_name": "xxx",
            "country_code": "101",
            "province_code": "101101",
            "city_code": "101101101",
            "store_address": "xxx",
            "brand_count": 1,
            "user_id": 1,
            "store_status": 1,
        }

        @apiSuccessExample {json} Success-Response:
        {
            "status":0,
            "message":"请求成功",
            "data":{}
        }
        """
        model = models.StoreInfoModel(self, store_id)
        res = await model.insert_info(self.form_data)
        self.finish_auto(**res)

    @check_login()
    @check_form(forms.AddStoreForm)
    async def put(self, store_id):
        """
        @api {PUT} /api/v1.0/demo/store/:store_id 修改门店基本信息
        @apiVersion 1.0.0
        @apiName UpdateStoreInfo
        @apiGroup DemoStore
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) store_name  门店名称
        @apiParam (Request-params) country_code  区域编码-国家
        @apiParam (Request-params) province_code  区域编码-省份
        @apiParam (Request-params) city_code  区域编码-城市
        @apiParam (Request-params) store_address  门店地址
        @apiParam (Request-params) brand_count  品牌数量
        @apiParam (Request-params) user_id  门店管理员
        @apiParam (Request-params) store_status  门店状态

        @apiParamExample {json} Request-Example:
        {
            "store_name": "xxx",
            "country_code": "101",
            "province_code": "101101",
            "city_code": "101101101",
            "store_address": "xxx",
            "brand_count": 1,
            "user_id": 1,
            "store_status": 1,
        }

        @apiSuccessExample {json} Success-Response:
        {
            "status":0,
            "message":"请求成功",
            "data":{}
        }
        """
        model = models.StoreInfoModel(self, store_id)
        res = await model.update_info(self.form_data)
        self.finish_auto(**res)

    @check_login(["delete_store"])
    async def delete(self, store_id):
        """
        @api {DELETE} /api/v1.0/demo/store/:store_id 删除门店
        @apiVersion 1.0.0
        @apiName DeleteStoreInfo
        @apiGroup DemoStore
        @apiDescription 如题
        @apiPermission null
        @apiUse RequestHeader

        @apiParam (Request-params) goods_id  商品ID

        @apiSuccessExample {json} Success-Response:
        {
            "status":0,
            "message":"请求成功",
            "data":{}
        }
        """
        model = models.StoreInfoModel(self, store_id)
        res = await model.delete_info()
        self.finish_auto(**res)
